package com.have.blog.admin.redis.impl;

import com.have.blog.admin.constant.AdminRedisConstant;
import com.have.blog.admin.redis.AdminRoleCacheService;
import com.have.blog.config.service.RedisService;
import com.have.blog.model.models.ResourceMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created By Have
 * 2021/7/17 19:14
 */
@Service
public class AdminRoleCacheServiceImpl implements AdminRoleCacheService {
    @Value("${redis.admin.database}")
    private String REDIS_DATABASE;
    @Autowired
    private RedisService redisService;

    @Override
    public void setRoleResource(int roleId, List<ResourceMenu> resources, long expireTime, TimeUnit timeUnit) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.ROLE + AdminRedisConstant.SEGMENTATION
                + roleId;
        redisService.set(key, resources, expireTime, timeUnit);
    }

    @Override
    public List<ResourceMenu> getRoleResource(int roleId) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.ROLE + AdminRedisConstant.SEGMENTATION
                + roleId;
        return (List<ResourceMenu>) redisService.get(key);
    }

    @Override
    public void delRoleResource(int roleId) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.ROLE + AdminRedisConstant.SEGMENTATION
                + roleId;
        redisService.del(key);
    }

    @Override
    public void setRoleIdByRoleName(String roleName, int roleId) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.ROLE_NAME + AdminRedisConstant.SEGMENTATION
                + roleName;
        redisService.set(key, roleId);
    }

    @Override
    public Integer getRoleId(String roleName) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.ROLE_NAME + AdminRedisConstant.SEGMENTATION
                + roleName;
        return (Integer) redisService.get(key);
    }

    @Override
    public void delRoleIdByRoleName(String roleName) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.ROLE_NAME + AdminRedisConstant.SEGMENTATION
                + roleName;
        redisService.del(roleName);
    }
}
